package com.guigu.controller.manufacture;


import com.github.pagehelper.PageInfo;
import com.guigu.pojo.design.DFile;
import com.guigu.pojo.manufacture.MApply;
import com.guigu.service.design.DFileService;
import com.guigu.service.manufacture.MApplyService;
import com.guigu.utils.CodeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mr.Zhang
 * @since 2024-12-06
 */
@Controller
@RequestMapping("/m-apply")
public class MApplyController {

    @Autowired
    private DFileService dFileService;

    @Autowired
    private MApplyService mApplyService;

    @GetMapping("/Add_new_production")
    public String Add_new_production(){
        return "jsp/manufacture/d/Add_new_production";
    }

    @GetMapping("/Formulate_production_plan")
    public String Formulate_production_plan(){
        return "jsp/manufacture/d/Formulate_production_plan";
    }

    @GetMapping("/Inquiry_production_plan")
    public String Inquiry_production_plan(){
        return "jsp/manufacture/d/Inquiry_production_plan";
    }

    @GetMapping("/Inquiry_producttion_plan_query")
    public String Inquiry_producttion_plan_query(){
        return "jsp/manufacture/d/Inquiry_producttion_plan_query";
    }

    @GetMapping("/New_production_plan_registration")
    public String New_production_plan_registration(){
        return "jsp/manufacture/d/New_production_plan_registration";
    }

    @GetMapping("/New_production_plan_registration_preview")
    public String New_production_plan_registration_preview(){
        return "jsp/manufacture/d/New_production_plan_registration_preview";
    }

    @GetMapping("/production_plan_review")
    public String production_plan_review(){
        return "jsp/manufacture/d/production_plan_review";
    }

    @GetMapping("/production_plan_review_sub")
    public String production_plan_review_sub(){
        return "jsp/manufacture/d/production_plan_review_sub";
    }

    @GetMapping("/Formulate_product_plan_query")
    public String Formulate_product_plan_query(){
        return "jsp/manufacture/d/Formulate_product_plan_query";
    }

    @PostMapping("/formulatePlan")
    @ResponseBody
    public Boolean formulatePlan(@RequestBody ArrayList<Object> arr){
        System.out.println(arr);
        String registrants = "";
        String remarks = "";

        HashMap<String, Object> map = (HashMap<String, Object>) arr.get(0);
        if(map.containsKey("registrants")){
            registrants = map.get("registrants").toString();
        }
        if(map.containsKey("remarks")){
            remarks = map.get("remarks").toString();
        }
        boolean b = false;
        ArrayList<Object> arr2 = (ArrayList<Object>) arr.get(1);
        for(int i = 0;i<arr2.size();i++){
            HashMap<String, Object> map2 = (HashMap<String, Object>) arr2.get(i);
            String productId = map2.get("productId").toString();

            DFile dFile = dFileService.selectByProductId(productId);

            MApply endMApply = mApplyService.getEndMApply();
            CodeUtils codeUtils = new CodeUtils();
            String produceCode = codeUtils.getProduceCode(endMApply);

            MApply mApply = new MApply();
            mApply.setApplyId(produceCode);
            mApply.setProductId(dFile.getProductId());
            mApply.setProductName(dFile.getProductName());
            mApply.setProductDescribe(dFile.getProductDescribe());
            mApply.setType(dFile.getType());
            mApply.setAmount(BigDecimal.valueOf(Long.parseLong(map2.get("num").toString())));
            mApply.setDesigner(dFile.getRegister());
            mApply.setRemark(remarks);
            mApply.setRegister(registrants);
            mApply.setRegisterTime(new Date());
            mApply.setCheckTag("0");
            mApply.setManufactureTag("0");

            boolean save = mApplyService.save(mApply);
            if(!save){
                b = false;
                break;
            }else {
                b = true;
            }
        }
        return b;
    }

    @PostMapping("/initialPlanTable")
    @ResponseBody
    public PageInfo<MApply> initialPlanTable(@RequestBody HashMap<String, Object> map) {
        PageInfo<MApply> mApplyPageInfo = mApplyService.selectPageInfo(map);
        return mApplyPageInfo;
    }

    @GetMapping("/getMApplyById")
    @ResponseBody
    public MApply getMApplyById(HttpServletRequest req){
        Integer id = StringUtils.isNotBlank(req.getParameter("id")) ? Integer.parseInt(req.getParameter("id")) : null;
        MApply mApply = mApplyService.getById(id);
        return mApply;
    }

    @PostMapping("/auditPlan")
    @ResponseBody
    public Boolean auditPlan(@RequestBody MApply mApply){
        mApply.setCheckTime(new Date());
        boolean b = mApplyService.updateById(mApply);
        return b;
    }

}

